package com.financialDog.javaTool.controller.reportTools;

import com.alibaba.fastjson.JSONObject;
import com.financialDog.javaTool.config.ConfigManager;
import com.financialDog.javaTool.controller.accountManage.AccountManageController;
import com.financialDog.javaTool.controller.fileManage.ConfigManageController;
import com.financialDog.javaTool.controller.fileManage.ExportManageController;
import com.financialDog.javaTool.controller.fileManage.FileManageController;
import com.financialDog.javaTool.reportModel.ConfigVo;
import com.financialDog.javaTool.reportModel.FileVo;
import com.financialDog.javaTool.reportModel.KeyValue;
import com.financialDog.javaTool.utils.AnalyticOlapUtils;
import com.financialDog.javaTool.view.reportTools.ReportMergeToolView;
import com.financialDog.javaTool.services.reportTools.ReportMergeToolService;
import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
import com.xwintop.xcore.util.javafx.TooltipUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;

@Getter
@Setter
@Slf4j
public class ReportMergeToolController extends ReportMergeToolView {
    private ReportMergeToolService reportMergeToolService = new ReportMergeToolService(this);

    private ObservableList<Map<String, String>> tableData = FXCollections.observableArrayList();

    private ObservableList<Map<String, String>> configTableData = FXCollections.observableArrayList();

    private HashMap<KeyValue, String> accountTableData ;
    private AnalyticOlapUtils analyticOlapUtils = new AnalyticOlapUtils();
    private JSONObject accountJsonObject;
    final static String tableColumnName = "name";
    final static String tableColumnType = "type";
    final static String tableColumnYear = "yearType";
    final static String tableColumnFilePath = "filePath";

    private ConfigManager configManager = ConfigManager.getInstance();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initView();
        initEvent();
        initService();
    }

    private void initView() {
        configManager.generateFileTableData(tableData);
    }

    private void initEvent() {
    }

    private void initService() {
        accountJsonObject = reportMergeToolService.initAccountObjectCache();
    }


    private boolean isReady(){
        if (tableData == null || tableData.size() == 0){
            TooltipUtil.showToast("请先选择文件");
            return false;
        }
        return true;
    }

    @FXML
    private void validateReport(ActionEvent event) {
        if (!isReady()){
            return;
        }
        //先添加  后期 要更改
        analyticOlapUtils.initialOlap();
        for (Map<String, String> hashMap : tableData){
            String filePath = hashMap.get(tableColumnFilePath);
            String type = hashMap.get(tableColumnType);
            analyticOlapUtils.addOlapData(filePath, type, hashMap.get(tableColumnYear));
        }
        //先进行单文件校验
        this.getReportMergeToolService().setContext(analyticOlapUtils);
        this.analyticOlapUtils.validateBalanceData();
        this.analyticOlapUtils.validateProfitData();
        this.analyticOlapUtils.validateMoneyData();
    }

    @FXML
    private void startMergeTool(ActionEvent event) {
        if (!isReady()){
            return;
        }
        //存储数据
        analyticOlapUtils.initialOlap();
        for (Map<String, String> hashMap : tableData){
            String filePath = hashMap.get(tableColumnFilePath);
            String type = hashMap.get(tableColumnType);
            analyticOlapUtils.addOlapData(filePath, type, hashMap.get(tableColumnYear));
        }

        //生成报表
        this.getReportMergeToolService().setContext(analyticOlapUtils);
        this.reportMergeToolService.generateBalanceDataTableView();
        this.reportMergeToolService.generateProfitDataTableView();
        this.reportMergeToolService.generateMoneyDataTableView();
    }

    @FXML
    private void startGenerateAccount(ActionEvent event) throws IOException {
        if (!isReady()){
            return;
        }
        //存储数据
        analyticOlapUtils.initialOlap();
        for (Map<String, String> hashMap : tableData){
            String filePath = hashMap.get(tableColumnFilePath);
            String type = hashMap.get(tableColumnType);
            analyticOlapUtils.addOlapData(filePath, type, hashMap.get(tableColumnYear));
        }
        //生成报表
        this.getReportMergeToolService().setContext(analyticOlapUtils);
        if (accountTableData == null){
            AccountManageController accountManageController = new AccountManageController();
            accountManageController.setAccountCache(this.accountJsonObject);  //生成缓存 保存选择的指标
            setAccountTableData(accountManageController.getTableData());
        }
        this.reportMergeToolService.generateAccountTableView(accountTableData);
    }

    @FXML
    private void openFileManage(ActionEvent event) throws IOException {
        FXMLLoader fXMLLoader = FileManageController.getFXMLLoader();
        Parent root = fXMLLoader.load();
        FileManageController fileManageController = fXMLLoader.getController();
        fileManageController.setReportMergeToolController(this);
        setTableData(fileManageController.getTableData());
        JavaFxViewUtil.openNewWindow("文件管理列表", root);
    }

    @FXML
    private void openAccountManage(ActionEvent event) throws IOException {
        FXMLLoader fXMLLoader = AccountManageController.getFXMLLoader();
        Parent root = fXMLLoader.load();
        AccountManageController accountManageController = fXMLLoader.getController();
        accountManageController.setAccountCache(this.accountJsonObject);  //生成缓存 保存选择的指标
        accountManageController.setReportMergeToolController(this);
        setAccountTableData(accountManageController.getTableData());
        JavaFxViewUtil.openNewWindow("指标管理列表", root);
        //JavaFxViewUtil.openNewWindow("指标管理列表", null,root, 100.0,100.0, false, false, false);
    }

    @FXML
    private void openConfigManager(ActionEvent event) throws IOException {
        FXMLLoader fXMLLoader = ConfigManageController.getFXMLLoader();
        Parent root = fXMLLoader.load();
        ConfigManageController configManageController = fXMLLoader.getController();
        configManageController.setReportMergeToolController(this);
        //setAccountTableData(accountManageController.getTableData());
        JavaFxViewUtil.openNewWindow("项目管理", root);
        //JavaFxViewUtil.openNewWindow("指标管理列表", null,root, 100.0,100.0, false, false, false);
    }

    @FXML
    private void saveConfig(ActionEvent event) throws IOException{
        ConfigVo configVo = configManager.getCurrentConfig();
        List<FileVo> fileVoList = new ArrayList<>();
        tableData.forEach(e->{
            String fileName = e.get("name");
            String type = e.get("type");
            String year = e.get("yearType");
            String time = e.get("time");
            String filePath = e.get("filePath");
            FileVo fileVo = new FileVo();
            fileVo.setFileName(fileName);
            fileVo.setFilePath(filePath);
            fileVo.setTime(time);
            fileVo.setYear(year);
            fileVo.setType(type);
            fileVoList.add(fileVo);
        });
        configVo.setFileList(fileVoList);
        configManager.saveToFile();
    }

    @FXML
    private void startExportExcel(ActionEvent event) throws IOException {
        FXMLLoader fXMLLoader = ExportManageController.getFXMLLoader();
        Parent root = fXMLLoader.load();
        ExportManageController exportManageController = fXMLLoader.getController();
        exportManageController.setReportMergeToolController(this);
        JavaFxViewUtil.openNewWindow("导出管理", null, root, 300.0,400.0, false, false, false);
        // 每次写100行数据，就刷新数据出缓存
    }

    //设置传输数据
    public void setTableData(ObservableList<Map<String, String>> tableData){
        this.tableData = tableData;
        totalFileNum.setText(String.valueOf(this.tableData.size()));
    }

    public void setAccountTableData(HashMap<KeyValue, String> tableData){
        this.accountTableData = tableData;
    }

    public void setConfigTableData(ObservableList<Map<String, String>> tableData){
        this.configTableData = tableData;
    }

}